package com.jn.login;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jn.common.base.BaseActivity;
import com.jn.common.event.LoginEvent;
import com.jn.common.event.UserEvent;
import com.jn.common.provider.ILoginProvider;

import com.jn.common.service.ILoginService;
import org.greenrobot.eventbus.EventBus;

@Route(path = "/login/LoginActivity")
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private String from;
    @Autowired(name = "from")
    String from2;

    @Autowired
    ILoginProvider mProvider;
    private Button mLoginButton;
    private EditText mUserNameEt;
    private EditText mPasswordEt;
    private String username;
    private String password;
    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void init() {
        ARouter.getInstance().inject(this);
        Intent intent = getIntent();
        String from = intent.getStringExtra("from");
        showToast(from2);
        mLoginButton = findViewById(R.id.login_button);
        mLoginButton.setOnClickListener(this);
        mUserNameEt = findViewById(R.id.et_student_username);
        mPasswordEt = findViewById(R.id.et_student_password);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.login_button) {
            //Provider 方式
            if (mProvider == null) {
                //mProvider = ARouter.getInstance().navigation(ILoginProvider.class);
                mProvider = (ILoginProvider) ARouter.getInstance().build("/login/LoginProvider").navigation();
            }
            mProvider.setLoginStatus(true);
            //EventBus方式
            //EventBus.getDefault().post(new LoginEvent(true));
            //Service方式
            ILoginService loginService = (ILoginService) ARouter.getInstance()
                    .build("/login/service")
                    .navigation();
            if (loginService != null) {
                loginService.setLogin(true);
            }

            username = mUserNameEt.getText().toString();
            password = mPasswordEt.getText().toString();
            //postUser();
            Intent intent = new Intent();
            intent.putExtra("tab", from2);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    private void postUser() {
        Message message = Message.obtain();
        handler.sendMessageDelayed(message, 500);
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            //EventBus发送用户信息
            EventBus.getDefault().post(new UserEvent(username, password));
            return false;
        }
    });

}package com.jn.login;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jn.common.base.BaseActivity;
import com.jn.common.event.LoginEvent;
import com.jn.common.event.UserEvent;
import com.jn.common.provider.ILoginProvider;

import org.greenrobot.eventbus.EventBus;

@Route(path = "/login/LoginActivity")
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private String from;
    @Autowired(name = "from")
    String from2;

    @Autowired
    ILoginProvider mProvider;
    private Button mLoginButton;
    private EditText mUserNameEt;
    private EditText mPasswordEt;
    private String username;
    private String password;
    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void init() {
        ARouter.getInstance().inject(this);
        Intent intent = getIntent();
        String from = intent.getStringExtra("from");
        showToast(from2);
        mLoginButton = findViewById(R.id.login_button);
        mLoginButton.setOnClickListener(this);
        mUserNameEt = findViewById(R.id.et_student_username);
        mPasswordEt = findViewById(R.id.et_student_password);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.login_button) {
            if (mProvider == null) {
                //mProvider = ARouter.getInstance().navigation(ILoginProvider.class);
                mProvider = (ILoginProvider) ARouter.getInstance().build("/login/LoginProvider").navigation();
            }
            mProvider.setLoginStatus(true);
            //EventBus发送登录事件
            EventBus.getDefault().post(new LoginEvent(true));
            username = mUserNameEt.getText().toString();
            password = mPasswordEt.getText().toString();
            postUser();
            Intent intent = new Intent();
            intent.putExtra("tab", from2);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    private void postUser() {
        Message message = Message.obtain();
        handler.sendMessageDelayed(message, 500);
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            //EventBus发送用户信息
            EventBus.getDefault().post(new UserEvent(username, password));
            return false;
        }
    });

}